package com.doit.zhaohang.handlerlunbotu;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Created by 赵航 on 2017/5/18.
 */

public class YuKu extends RelativeLayout implements View.OnClickListener {

    private ImageView      mHome;
    private ImageView      mMenu;
    private RelativeLayout mLevel3;
    private RelativeLayout mLevel2;
    private RelativeLayout mLevel1;
    private boolean leve3 = true;
    private boolean leve2 = true;
    private RotateAnimation mRotateAnimation;
    private RotateAnimation mRotateAnimation1;


    public YuKu(Context context) {
        this(context, null);
    }

    //必先走这个函数
    public YuKu(Context context, AttributeSet attrs) {
        super(context, attrs);
        /*
        * 1.编写好布局
        *
        * 2.使用布局填充器，将menu对象做为view植入布局中
        *
        * 3.将view添加到当前控件中
        * */

        //该为植入布局
        LayoutInflater.from(context).inflate(R.layout.yk_layout, this, true);
        //初始化控件
        mHome = (ImageView) findViewById(R.id.iv_home);
        mMenu = (ImageView) findViewById(R.id.iv_menu);

        mLevel3 = (RelativeLayout) findViewById(R.id.yu_level3);
        mLevel2 = (RelativeLayout) findViewById(R.id.yu_level2);
        mLevel1 = (RelativeLayout) findViewById(R.id.yu_level3);

        //给图片设置点击事件
        mHome.setOnClickListener(this);
        mMenu.setOnClickListener(this);

    }

    public YuKu(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }


    //最低兼容是16，从android5.0开始实行的
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public YuKu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_home:

                if (leve3){

                    hidMenu(mLevel3,0);

                    //TOdo:先隐藏3再隐藏2；
                    hidMenu(mLevel2,250);

                    //直接等于false这样的话，弄成ture比较好
                   leve3 = false;

                }else if (leve2){

                    //TODO:隐藏2；
                    hidMenu(mLevel2,0);

                }else {
                    //TODO:显示2
                    showMenu(mLevel2);
                }

                leve2 = !leve2;
                break;
            case R.id.iv_menu:
                if (leve3) {

                    //                    TODO:mLevel3执行隐藏操作

                    hidMenu(mLevel3,0);
                } else {

                    //                    TODO:mLevel3执行显示操作

                    showMenu(mLevel3);
                }

                //把自己的值给记录下来
                leve3 = !leve3;
                //接口回调固定格式
                if (mOnMenuChengListener!=null){
                    mOnMenuChengListener.onCheng(leve3,leve2);
                }
                break;
        }

    }

    private void showMenu(RelativeLayout level3) {
        //补间动画可以围绕任何方向转动
        mRotateAnimation1 = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);

        //设置动画执行时间
        mRotateAnimation1.setDuration(500);

        //保持动画执行后的样子
        mRotateAnimation1.setFillAfter(true);

        //设置不可用
        mMenu.setEnabled(true);


        //开始执行动画
        level3.startAnimation(mRotateAnimation1);


    }

    private void hidMenu(RelativeLayout level3,long delay) {
        //补间动画可以围绕任何方向转动
        mRotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);

       //设置动画执行时间
        mRotateAnimation.setDuration(500);

        //保持动画执行后的样子
        mRotateAnimation.setFillAfter(true);

        //让动画执行多少秒以后来执行
        mRotateAnimation.setStartOffset(delay);

        //开始执行动画
        level3.startAnimation(mRotateAnimation);

        //设置不可用
        mMenu.setEnabled(false);
    }
    //回调接口,固定写法
    public interface  OnMenuChengListener{
        void onCheng(boolean isLeve3,boolean isLeve2);
    }

    private OnMenuChengListener mOnMenuChengListener;

    public void setOnMenuChengListener(OnMenuChengListener onMenuChengListener){
        this.mOnMenuChengListener = onMenuChengListener;
    }
}
